<template>
  <el-row class="login_container">
    <el-col :lg="16" :md="12" class="left">
      <h1 class="left_title">欢迎光临</h1>
    </el-col>
    <el-col :lg="8" :md="12" class="right">
      <h2 class="right_title">欢迎回来</h2>
      <div class="login_top">
        <span></span>
        <span>账号密码登录</span>
        <span></span>
      </div>
      <el-form :model="ruleForm" class="w-[250px]" :rules="rules" ref="ruleFormRef">
        <el-form-item prop="userName">
          <el-input
            v-model="ruleForm.userName"
            placeholder="请输入用户名"
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            :prefix-icon="Unlock"
            :suffix-icon="Wiew"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button
            round
            color="#626aef"
            class="w-[250px]"
            type="primary"
            :loading="loading"
            @click="submitForm(ruleFormRef)"
            >登 录</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted, onBeforeUnmount} from "vue";
import type { FormInstance, FormRules,} from "element-plus";
import { useRouter } from "vue-router";
import { User, Unlock } from "@element-plus/icons-vue";
import { toast } from "~/composables/util";
import { useStore } from "vuex";
const store = useStore();
const router = useRouter();
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  userName: "",
  passWord: "",
});

const rules = reactive<FormRules>({
  userName: [{ required: true, message: "用户名输入错误请重新输入", trigger: "blur" }],
  passWord: [{ required: true, message: "密码输入错误请重新输入", trigger: "blur" }],
});
const loading = ref(false);
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true;
      store
        .dispatch("login", ruleForm)
        .then((res) => {
          toast("登录成功");
          router.push("/");
        })
        .finally(() => {
          loading.value = false;
        });
    } else {
      console.log("error submit!", fields);
    }
  });
};
// 监听回车事件
function onkeyUp(e) {
  if (e.code === 'Enter') {
    console.log(111);
    submitForm(ruleFormRef.value);
  }
}
onMounted(() => {
  // 添加监听键盘事件
  document.addEventListener("keyup", onkeyUp);
});
onBeforeUnmount(() => {
  // 移除键盘事件
  document.removeEventListener("keyup", onkeyUp);
});
</script>

<style scope lang="scss">
.login_container {
  @apply min-h-screen bg-light-500;
}
.left {
  @apply bg-blue-500;
  .left_title {
    @apply text-light-800 text-4xl;
  }
}
.right {
  @apply bg-light-50 flex-col;
  .right_title {
    @apply font-bold text-3xl text-gray-800;
  }
}
.login_top {
  @apply my-5 text-gray-300 space-x-2;
  span:first-child,
  span:last-child {
    @apply h-[1px] w-16 bg-gray-200;
  }
}
.left,
.right,
.login_top {
  @apply flex justify-center items-center;
}
</style>
